<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@include file="/ui/jsp/includes/includes.jspf"%>

<%@include file="/ui/jsp/includes/header.jspf"%>
<div class="hero-unit secondary_container">
    <div id="table-subject-header">
        <c:if test="${!empty userLogin.userLoginId}">
            Update User
        </c:if>
        <c:if test="${empty userLogin.userLoginId}">
            Add New User
        </c:if>	
    </div>
    <hr>

    <div class="row-fluid">
        <form:form method="POST" id="addNewUserForm"
                   commandName="userLogin" class="form-horizontal">
            <form:errors path="*" cssClass="errorblock" element="div"/>
            <div class="control-group">
                <label class="control-label" for="facultyId">
                    Faculty Member* </label>
                <div class="controls">
                    <input type="text" name="facultyName" id="facultyName" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="userRoleId">User Role* </label>
                <div class="controls">
                    <form:select id="userRoleId" path="userRole.userRoleId">
                        <form:option value="0" label="Select" />
                        <form:options items="${userRoleList}" itemValue="userRoleId"
                                      itemLabel="roleName" />
                    </form:select>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="userName">User Name* </label>
                <div class="controls">
                    <form:input id="userName" path="userName" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="password">Password* </label>
                <div class="controls">
                    <form:input id="password" type="password" path="password" />
                </div>
            </div>
            <div>*Required Fields</div>
            <div class="control-group">
                <div class="controls">
                    <c:choose>
                        <c:when test="${!empty userLogin.userLoginId}">
                            <form:hidden path="userLoginId" id="userLoginId"/>
                            <input type="button" onclick="javascript:updateUser()" value="Submit" class="btn btn-primary"/>
                        </c:when>
                        <c:otherwise>
                            <input type="button" onclick="javascript:addNewUser()" value="Submit" class="btn btn-primary"/>
                        </c:otherwise>
                    </c:choose>
                    <input type="button" value="Cancel" onclick="javascript:backToUserList()" class="btn btn-primary"/>
                </div>
            </div>
            <form:hidden id="user_faculty" path="faculty.facultyId" />
            <form:hidden id="firstTimeLogin" path="firstTimeLogin" value="" />
            <form:hidden id="securityQuestion" path="securityQuestion" value=""/>
            <form:hidden id="securityAnswer" path="securityAnswer" value=""/>
        </form:form>
    </div>
</div>
<%@include file="/ui/jsp/includes/footer.jspf"%>

<script type="text/javascript">
                                function addNewUser()
                                {
                                    $('#addNewUserForm').attr('action', 'addNewUser.html');
                                    $('#addNewUserForm').submit();
                                }
                                function updateUser()
                                {
                                    $('#userId').val('${userLogin.userLoginId}');
                                    $('#addNewUserForm').attr('action', 'updateUser.html');
                                    $('#addNewUserForm').submit();
                                }
                                function backToUserList()
                                {
                                    $('#addNewUserForm').attr('action', 'viewUsersList.html');
                                    $('#addNewUserForm').submit();
                                }
                                $(function() {
                                    var facultyList = '${facultyList}';
                                    //alert(facultyList);
                                    var facultyStr = facultyList.substring(facultyList.indexOf('{') + 1,
                                            facultyList.indexOf('}'));
                                    facultyStr = facultyStr.replace(/=/g, ":");
                                    var facultyArr = facultyStr.split(',');
                                    var tempArray = new Array();
                                    var tempStr = "";
                                    for (var i = 0; i < facultyArr.length; i++) {
                                        tempStr = facultyArr[i];
                                        tempStr = tempStr.substring(tempStr.indexOf(":") + 1);
                                        tempArray[i] = tempStr;
                                    }
                                    $("#facultyName").autocomplete({
                                        source: tempArray,
                                        select: function(e, ui) {
                                            //create formatted data
                                            var faculty = ui.item.value;
                                            getFacId(faculty, facultyArr);
                                            $("#facultyName").val(faculty);
                                        },
                                        close: function(event, ui) {
                                        }
                                    });
                                });

                                function getFacId(str, arr) {
                                    var key = "";
                                    var value = "";
                                    var tempStr = "";
                                    for (var i = 0; i < arr.length; i++) {
                                        tempStr = arr[i];
                                        tempStr = tempStr.replace(/^\s+|\s+$/g, '');
                                        key = tempStr.substring(0, tempStr.indexOf(":"));
                                        value = tempStr.substring(tempStr.indexOf(":") + 1);
                                        if (value == str) {
                                            $("#user_faculty").val($.trim(key));
                                        }
                                    }
                                }
                                $(document).ready(function() {
                                    $("#facultyName").val('${userLogin.faculty.fullName}');
                                });


</script>
